//
//  UIButton-Ex.swift
//  weibo1990
//
//  Created by 曹晓龙 on 15/10/17.
//  Copyright © 2015年 曹晓龙. All rights reserved.
//

import UIKit

extension UIButton {
    
    ///  遍历构造函数
    ///
    ///  - parameter imageName:     图片名称
    ///  - parameter backImageName: 背景图片名称
    ///
    ///  - returns: UIButton
    convenience init (imageName:String, backImageName:String?) {
        
        self.init()
        
        setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        
        if let backImageName = backImageName {
            
            setBackgroundImage(UIImage(named: backImageName), forState: UIControlState.Normal)
            setBackgroundImage(UIImage(named: "\(backImageName)_highlighted"), forState: UIControlState.Highlighted)
        }
        
        
        sizeToFit()
    }
    
    ///  遍历构造函数
    ///
    ///  - parameter title:         按钮文字
    ///  - parameter backImageName: 背景图片
    ///  - parameter backColor:     背景颜色 - 默认不传颜色
    ///  - parameter titleColor:    文字颜色--默认黑色
    ///
    ///  - returns: UIButton
    convenience init(title:String, backImageName:String?, backColor:UIColor? = nil, titleColor:UIColor = UIColor.darkGrayColor()){
        self.init()
        setTitle(title, forState: UIControlState.Normal)
        
        if let backImageName = backImageName {
            
            setBackgroundImage(UIImage(named: backImageName), forState: UIControlState.Normal)
        }
        
        backgroundColor = backColor
        
        setTitleColor(titleColor, forState: UIControlState.Normal)
        }
    
    ///  遍历构造函数
    ///
    ///  - parameter title:         按钮文字
    ///  - parameter imageName:     图片名
    ///  - parameter titleColor:    文字颜色--默认黑色
    ///
    ///  - returns: UIButton
    convenience init(title:String, imageName:String, titleColor:UIColor = UIColor.darkGrayColor()){
        self.init()
        setTitle(title, forState: UIControlState.Normal)
        setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        setTitleColor(titleColor, forState: UIControlState.Normal)
    }
    
}
